// 帖子模块
/*----------依赖模块--------- */
const express = require('express');
const router = express.Router();
const db = require('../db/db');
const back = require('../utils/back');
const md5 = require('md5');
const middle = require('../middle/auth');
const dayjs = require('dayjs');
const token = require('../controller/token')
/*----------API方法---------- */

const post = {}

//创建帖子
post.creatPost = async (req, res) => {
    const params = req.body;
    const { user_id } = req.userInfo;

    // 参数是否齐全
    if (!params.title || !params.content) {
        // 如果参数不全，要返回给客户端一个内容
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, '上传的参数不全'))
        return;
    }

    // 对时间格式进行处理
    const d = params.time ? dayjs(params.time) : dayjs();
    const publish_time = d.format('YYYY-MM-DD HH:mm:ss');

    // 插入新的举报信息
    const state = await db.query('insert into post (user_id,title,content,publish_time) values(?,?,?,?)',
        [user_id, params.title, params.content, publish_time]);
    if (state === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '插入失败'));
        return;
    }
    console.log(state);

    //查询插入帖子
    const postMessage = await db.query('select *from post where post_id=?', [state.insertId])
    res.send(back.Data(postMessage))
}
//获取所有帖子
post.allpost = async (req, res) => {
    const params = req.query;
    const allData = await db.query('select *from post');
    if (allData === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }
    res.send(back.Data(allData))
}
//获取帖子和评论
post.getPost = async (req, res) => {
    const params = req.query;
    // 参数是否齐全
    if (!params.post_id) {
        // 如果参数不全，要返回给客户端一个内容
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, '上传的参数不全'))
        return;
    }

    const postMessage = await db.query('select * from post where post_id=?', [params.post_id])
    if (postMessage === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }
    if (postMessage.length < 1) {
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, '无此帖子'))
        return;
    }

    const postComment = await db.query('select * from comment where post_id=?', [params.post_id])
    if (postComment === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '查询失败'));
        return;
    }
    const postData = {
        postMessage: postMessage,
        postComment: postComment
    }
    res.send(back.Data(postData))
}

post.addComment = async (req, res) => {
    const params = req.body;
    const { user_id } = req.userInfo;

    // 参数是否齐全
    if (!params.post_id || !params.content) {
        // 如果参数不全，要返回给客户端一个内容
        res.send(back.ERR(back.ERRTYPE.ERROR_PARAM, '上传的参数不全'))
        return;
    }

    // 对时间格式进行处理
    const d = params.time ? dayjs(params.time) : dayjs();
    const comment_time = d.format('YYYY-MM-DD HH:mm:ss');

    const insert = await db.query('insert into comment (post_id,user_id,content,comment_time) VALUES (?,?,?,?)',
        [params.post_id, user_id, params.content, comment_time])
    if (insert === false) {
        res.send(back.ERR(back.ERRTYPE.ERROR_DB, '插入失败'));
        return;
    }

    //查询插入评论
    const commentMessage = await db.query('select * from comment where comment_id=?', [insert.insertId])
    res.send(back.Data(commentMessage))
}
/*---------路由---------- */
// 注册
router.post('/creatPost', middle.userAuth,post.creatPost)
router.get('/allpost', post.allpost)
router.get('/getPost', post.getPost)
router.post('/addComment', middle.userAuth,post.addComment)

/*-------暴露----------- */
module.exports = router;